﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Windows;
using System.Reflection;
using SAF.Framework.Controls.Properties;

namespace SAF.Framework.Controls.Upgrade
{
    internal class Helper
    {
        /// <summary>
        /// 重启主程序
        /// </summary>
        public static void RestartApplication()
        {
            string[] commandLineArgs = Environment.GetCommandLineArgs();
            StringBuilder builder = new StringBuilder((commandLineArgs.Length - 1) * 0x10);
            for (int i = 1; i < (commandLineArgs.Length - 1); i++)
            {
                builder.Append('"');
                builder.Append(commandLineArgs[i]);
                builder.Append("\" ");
            }
            if (commandLineArgs.Length > 1)
            {
                builder.Append('"');
                builder.Append(commandLineArgs[commandLineArgs.Length - 1]);
                builder.Append('"');
            }
            ProcessStartInfo startInfo = Process.GetCurrentProcess().StartInfo;
            startInfo.FileName = System.Reflection.Assembly.GetEntryAssembly().Location;
            if (builder.Length > 0)
            {
                startInfo.Arguments = builder.ToString();
            }

            Process.Start(startInfo);
            Environment.Exit(0);
        }

        public static T XmlString2Entity<T>(string xmlString)
        {
            if (xmlString == null || xmlString.Trim().Length <= 0)
            {
                throw new Exception(Resources.XmlStringIsNull);
            }

            Stream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xmlString));
            System.Xml.Serialization.XmlSerializer XmlSerializer = new XmlSerializer(typeof(T));
            try
            {
                return (T)XmlSerializer.Deserialize(stream);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static T XmlFile2Entity<T>(string xmlPath)
        {
            if (!File.Exists(xmlPath))
            {
                throw new Exception(xmlPath + Resources.NotExists);
            }

            System.IO.FileStream FS = null;
            System.Xml.Serialization.XmlSerializer XmlSerializer = new XmlSerializer(typeof(T));
            try
            {
                FS = new System.IO.FileStream(xmlPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                return (T)XmlSerializer.Deserialize(FS);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                FS.Close();
            }
        }
        /// <summary>
        /// 获取远程文件大小，单位：Byte
        /// </summary>
        /// <param name="sURL"></param>
        /// <returns></returns>
        public static long GetHTTPFileSize(string sURL)
        {
            long size = 0L;
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
                request.Method = "HEAD";
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                size = response.ContentLength;
                response.Close();
            }
            catch
            {
                size = 0L;
            }
            return size;
        }

        /// <summary>
        /// 程序的默认关闭方式
        /// </summary>
        public static ShutdownMode ApplicationShutDownMode = Application.Current.ShutdownMode;
    }
}
